package cn.hacynn.nd.controller;

import cn.hacynn.nd.common.result.Result;
import cn.hacynn.nd.pojo.entity.File;
import cn.hacynn.nd.service.SafeFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Hacynn
 * @Date 2024/5/27 23:12
 * @Version 1.0
 */
@RestController
@RequestMapping("/safe")
@Slf4j
public class SafeFileController {

    @Autowired
    private SafeFileService safeFileService;

    /**
     * 查询是否激活保险箱
     * @return
     */
    @GetMapping("/check")
    public Result<Boolean> check() {
        Boolean b = safeFileService.check();
        return Result.success(b);
    }

    /**
     * 查询是否进入过保险箱
     * @return
     */
    @GetMapping("/checkEntered")
    public Result<Boolean> checkEntered() {
        Boolean b = safeFileService.checkEntered();
        //返回一个结果呈现给用户观看
        return Result.success(b);
    }

    /**
     * 激活保险箱
     * @param safeCode
     * @return
     */
    @PostMapping("/activate")
    public Result activate(@RequestParam String safeCode) {
        log.info("激活保险箱，设置密钥:{}",safeCode);
        safeFileService.activate(safeCode);
        return Result.success();
    }

    /**
     * 校验保险箱密钥
     * @param safeCode
     * @return
     */
    @PostMapping("/match")
    public Result<Boolean> match(@RequestParam String safeCode) {
        log.info("校验保险箱密钥，密钥:{}",safeCode);
        Boolean b = safeFileService.match(safeCode);
        return Result.success(b);
    }

    /**
     * 查询保险箱中的文件列表信息
     * @return
     */
    @GetMapping("/list")
    public Result<List<File>> list() {
        List<File> files = safeFileService.list();
        return Result.success(files);
    }

    /**
     * 锁定保险箱
     * @return
     */
    @PostMapping("/lockSafe")
    public Result lockSafe() {
        safeFileService.lockSafe();
        return Result.success();
    }

    /**
     * 移入保险箱
     * @param ossPath
     * @return
     */
    @PostMapping("/moveToSafe")
    public Result moveToSafe(@RequestParam String ossPath) {
        log.info("移入保险箱，文件路径:{}", ossPath);
        safeFileService.moveToSafe(ossPath);
        return Result.success();
    }


    /**
     * 移出保险箱
     * @param ossPath
     * @return
     */
    @PostMapping("/moveOutSafe")
    public Result moveOutSafe(@RequestParam String ossPath) {
        log.info("移出保险箱，文件路径:{}", ossPath);
        safeFileService.moveOutSafe(ossPath);
        return Result.success();
    }
}
